<?
$zdir = 1;
include("../includes/functions.php");
include($pre."includes/sajax/sajaxFileUpload.php");

sajax_init();
$sajax_request_type = "POST"; 
sajax_export("getSubCategories");
sajax_handle_client_request();

$catID = $_GET['cID'];
$fCategories = getCategories('select','F',$catID);

// Look for sub-categories coming in
$fSubCats = getSubCategories('select','F',$catID);
if ($fSubCats == '0') {
	$subCatContainerDisplay = 'none';
} else {
	$subCatContainerDisplay = '';
}

if ($_POST[upload] == 1) {
	$fileCat    = $_POST[fileCat];
	$fileSubCat = $_POST[subCatSelect]; 
	$fileTitle  = fieldToDB($_POST[fileTitle]);
	$fileDesc   = fieldToDB($_POST[fileDesc]);
		
	$error = 0;
	$upload = 1;
		
	$someFile     = $_FILES['fileUpload']['tmp_name'];
	$someFileName = $_FILES['fileUpload']['name'];
	$someFileType = $_FILES['fileUpload']['type'];
	$someFileSize = $_FILES['fileUpload']['size'];
		
	// Clean Filename
	$filename = cleanString($someFileName);		
	$fileDir  = $rootDir.'docs/Shared Files/';
	$filePath = $rootDir.'docs/Shared Files/'.$filename;
	
	if ($error != 1) {
		if(move_uploaded_file($someFile, $filePath)){
			// Add file to database
			if ($_SESSION['userLevel'] == 1) {
				$approved = 1;
			} else {
				$approved = 0;
			}
			
			$sql = "INSERT into casa_shared_files (
					CatID,
					SubCatID,
					FileTitle,
					FileDesc,
					FileName,
					FileDate,
					FileSize,
					FileType,
					Approved
					) values (
					'$fileCat',
					'$fileSubCat',
					'$fileTitle',
					'$fileDesc',
					'$filename',
					'".date('Y-m-d')."',
					'$someFileSize',
					'$someFileType',					
					'$approved'
					)";
			$result = mysql_db_query($dbname,$sql);			
		} else {
			$error = 1;
		}
	}	
} else {
	$error = 0;
	$upload = 0;	
}

if (empty($fileCat)) {
	$fileCat = 0;
}
if (empty($fileSubCat)) {
	$fileSubCat = 0;
}
?>

<html><head>
<SCRIPT src="../js/protaculous/prototype.js" language="JavaScript" type=text/javascript></SCRIPT>
<script language="javascript">
var par = window.parent.document;

function uploadComplete(upload,error) {
	if (upload == 1) {
		var progressDiv = par.getElementById('uploadStatus<?=$catID; ?>');
		if (error == 1) {
			progressDiv.innerHTML   = '<img src="../images/iconError.gif"> <?=$errorMessage; ?>';
		} else {
			// Refresh file list
			window.parent.filesViewObj.refreshFileList(<?=$fileCat; ?>,<?=$fileSubCat; ?>);
			progressDiv.innerHTML   = '<img src="../images/iconStatusComplete.gif"> <b>Your Upload Was Successful!</b>';
		}
	}
}
function uploadFile(){	
	var iframe = par.getElementById('fileFrame');
	var form   = document.getElementById('iform');
			
	var progressDiv = par.getElementById('uploadStatus<?=$catID; ?>');
	progressDiv.innerHTML = '<img src="../images/ajaxAnimBar.gif"> Your file is uploading...';
		
	// send
	form.submit();	
}

function getSubCategories(catID) {
	var params = 'handler=getSubCategories&display=json&siteArea=F&catID='+catID;
	
	var ajaxResults = new Ajax.Request('functions.php', {
		method: 'POST',
		parameters: params,	
		onSuccess: function(transport) {
			returnGetSubCategories(transport.responseText);
		},
		onFailure: function() {
			alert('Oops!');
		}		
	});
}

function returnGetSubCategories(ajaxResult) {
	if (ajaxResult != '0') {
		var jsonObject = eval('(' + ajaxResult + ')');
		var totalCategories = (jsonObject.categories.length)-1;
		for(i=0;i<=totalCategories;i++) {
			var catID   = jsonObject.categories[i].CID;
			var catName = jsonObject.categories[i].CatName;
			$('subCatSelect').options[i] = new Option(catName,catID);
		}
		$('subCatContainer').style.display = '';
	} else {
		$('subCatContainer').style.display = 'none';
	}
}

<? sajax_show_javascript(); ?>
</script>
<SCRIPT src="<?=$pre; ?>js/jsCommon.js" type="text/javascript"></SCRIPT>
<SCRIPT src="<?=$pre; ?>js/protaculous/prototype.js" type="text/javascript"></SCRIPT>
<script language="JavaScript" type="text/javascript">
function jScriptInit() {
	// Turn on iFrame if necessary
	var par = window.parent.document;
	var iframe = par.getElementById('fileFrame<?=$catID; ?>');
	if (iframe.style.display == 'none') {
		iframe.style.display = 'block';
	}
}
</script>

<link rel="STYLESHEET" media="screen" href="<?=$pre; ?>css/stylesMozilla.css" type="text/css" />	
</head>
<body onload="jScriptInit(); uploadComplete(<?=$upload; ?>,<?=$error; ?>);">
<div class="blueBox">
	<form name="iform" action="<?=$_SERVER['PHP_SELF']; ?>?cID=<?=$catID; ?>" id="iform" method="post" enctype="multipart/form-data">
	<input type="hidden" name="upload" value="1">
		
	<input type="hidden" id="formAction" />
			<input type="hidden" id="fID" />			
			<p><span class="stupidVertLabel">Category</span>
				<select id="fileCat" name="fileCat" size="1" style="width: 400px;" onChange="getSubCategories(this.options[this.selectedIndex].value); return false;">
				<?=$fCategories; ?>
				</select>
			</p>	
			
			<p id="subCatContainer" style="display: <?=$subCatContainerDisplay; ?>;"><span class="stupidVertLabel">Sub-category</span>
			<select id="subCatSelect" size="1" style="width: 400px;" name="subCatSelect">
			<?=$fSubCats; ?>
			</select>
			</p>
			
			
			<p><span class="stupidVertLabel">Title</span>
			<input type="text" style="width: 400px;" id="fileTitle" name="fileTitle" /></p>
			
			<p><span class="stupidVertLabel">Short description</span>
			<textarea name="fileDesc" id="fileDesc" style="width: 400px; height: 100px;"></textarea>
			</p>			

			<p><span class="stupidVertLabel">Select File</span>
				<input type="file" id="fileUpload" name="fileUpload" size="50" />
			</p>			
			
			<p style="margin-top: 15px; margin-left: 6px;">
				<input type="button" class="buttonBig buttonSaveBig" onClick="uploadFile();" value="Save" title="Save file" />
				<input type="button" class="buttonBig buttonCancelBig" onClick="window.parent.filesSupportObj.closeAddEditForm(<?=$catID; ?>);" value="Cancel" title="Cancel" />
			</p>
			
	</form>
</div>	
</body>	
</html>